#include "../include/head.h"
#include "../include/func.h"

int init_cmd(char *buf, pCmd_t pcmd) {
    char *token = NULL;
    const char s[] = {' ', '\n'}; // 将分隔符设为空格和换行符

    token = strtok(buf, s); // 首次调用strtok，传入待分割的字符串和分隔符集合
    // 循环提取所有token
    while (token != NULL) {
        strcpy(pcmd->_cmd[pcmd->_argc], token); // 将当前token复制到pcmd的_cmd数组中
        pcmd->_argc++; // _argc记录参数个数
        token = strtok(NULL, s); // 后续调用传入 NULL，表示继续从上次位置分割
    }
    return 0;
}

int cmd_check(pCmd_t pcmd) {
    char tmp[31] = {0}; // 用于存放待判断的命令
    strncpy(tmp, pcmd->_cmd[0], 31); 
    // strcmp函数返回0表示两个字符串一致
    if (
        !strcmp(tmp, "rmdir") ||
        !strcmp(tmp, "cd") ||
        !strcmp(tmp, "ls") ||
        !strcmp(tmp, "pwd") ||
        !strcmp(tmp, "rm") ||
        !strcmp(tmp, "puts") ||
        !strcmp(tmp, "gets") ||
        !strcmp(tmp, "mkdir") ||
        !strcmp(tmp, "rmdir")) {
        return 0;
    } else {
        return -1;
    }
}

int print_cmd(pCmd_t pcmd) {
    printf("cmd: "); 
    for (int i = 0; i < pcmd->_argc; ++i) { // pcmd->_argc为参数个数
        printf("%s ", pcmd->_cmd[i]); // pcmd->_cmd[i] 为一个参数
    }
    printf("\n");
    return 0;
}

int upload(int fd_socket, char *filename)
{
    //查看文件类型是否正确
    DIR *dirp = opendir("./");
    struct dirent *dir_cur;
    int flag = -1; //是否传输标志, 0-不传，1-传
    while ((dir_cur = readdir(dirp)) != NULL) {
        if (!strcmp(dir_cur->d_name, filename)) {
            if (S_ISREG(dir_cur->d_type)) {
                //如果是普通文件, 传输
                flag = 1;
                send(fd_socket, &flag, sizeof(int), 0);
            }
            else {
                //如果不是普通文件，不传
                flag = 0;
                send(fd_socket, &flag, sizeof(int), 0);
            }
            break;
        }
    }
    closedir(dirp);

    if (0 == flag) {
        return -1;
    }

    int ret = -1;
    //定义一个小货车，用来传输文件
    Train_t train;
    memset(&train, 0, sizeof(train));

    //根据文件名打开传输文件
    int fd_file = open(filename, O_RDONLY);
    ERROR_CHECK(fd_file, -1, "open");

#if 0
    //发文件大小
    struct stat file_info;
    memset(&file_info, 0, sizeof(file_info));
    fstat(fd_file, &file_info);

    train._data_len = sizeof(file_info.st_size);
    memcpy(train._data, &file_info.st_size, train._data_len);
    ret = send(fd_socket, &train, sizeof(int) + train._data_len, 0);
    EXIT_CHECK(ret, -1, "send_filesize");
#endif

    //发文件内容
    while (1) {
        memset(train._data, 0, sizeof(train._data));

        //读取文件
        train._data_len = read(fd_file, train._data, BUF_SIZE);
        if (0 == train._data_len) {
            //传输完成，退出循环
            break;
        }

        //发送
        ret = send(fd_socket, &train, sizeof(int) + train._data_len, 0);
        if (-1 == ret) {
            //服务器异常断开，退出循环
            printf("server already break!\n");
            break;
        }
    }

    //传输完成，通知fd
    train._data_len = 0;
    send(fd_socket, &train._data_len, sizeof(int), 0);

    //关闭传输文件
    close(fd_file);
    return 0;
}

//下载文件
int download(int fd_socket, char *filename)
{
    //先接收传输标志，如果是1就下载，如果是0就退出
    int flag = -1;
    recv(fd_socket, &flag, sizeof(int), 0);
    if (0 == flag) {
        return -1;
    }

    //打开或创建一个文件
    int fd_file = open(filename, O_WRONLY | O_CREAT, 0600);
    ERROR_CHECK(fd_file, -1, "open");

    /* //接收文件大小 */
    /* int filesize = 0; */
    /* recv(fd_socket, &filesize, sizeof(int), 0); */
    /* printf("filesize: %d", filesize); */

    //接收文件内容
    Train_t train;
    while (1) {
        memset(&train, 0, sizeof(train));
        //接收数据长度
        recv(fd_socket, &train._data_len, sizeof(train._data_len), 0);
        if (0 == train._data_len) {
            //文件传输完毕
            break;
        }
        //接收数据内容
        recv(fd_socket, train._data, train._data_len, MSG_WAITALL);
        write(fd_file, train._data, train._data_len);
    }

    close(fd_file);
    return 0;
}
